﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference name="Telerik.Web.UI.Common.Core-vsdoc.js" assembly="Telerik.Web.UI" />
/// <reference path="~/Clients/Scripts/jQuery/jquery-1.6.1-vsdoc.js"/>
/// <reference path="~/Clients/Scripts/jQuery/jquery-ui-1.8.13.custom.min.js"/>
/// <reference path="~/Clients/Scripts/jQuery/jquery.ajaxmanager-3.12.debug.js"/>
/// <reference path="~/Clients/Scripts/jQuery/jquery.json.debug.js"/>
/// <reference path="~/Clients/Scripts/jQuery/jquery.component.debug.js"/>
/// <reference path="~/Clients/Scripts/globals.debug.js"/>
/// <reference path="~/Clients/Scripts/utils.debug.js"/>
/// <reference path="~/Clients/Scripts/errorHandling.debug.js"/>
/// <reference path="~/Clients/Scripts/loadingPanels.debug.js"/>
/// <reference path="~/Clients/Scripts/common.debug.js"/>
/// <reference path="~/Clients/Scripts/jqueryLookup.debug.js"/>
/// <reference path="~/Clients/Scripts/url.debug.js"/>
/// <reference path="~/Clients/Scripts/queryObject.debug.js"/>

Type.registerNamespace("IRepairClouds.Controls.Common");

IRepairClouds.Controls.Common.RadComboCheckBoxList = function(element) {
    IRepairClouds.Controls.Common.RadComboCheckBoxList.initializeBase(this, [element]);
    this._controlsHolder;
    this._onLoadDelegate;
}

IRepairClouds.Controls.Common.RadComboCheckBoxList.prototype = {
    _controls: function () {
        if (!this._controlsHolder) {
            this._controlsHolder = new (function (element) {
                this.jq = new GreeNova.JqueryLookup(element);
                this.HiddenFieldClient = new GreeNova.JqueryLookup(this.jq, "[id$='HiddenFieldClient']");
                this.RadTextBoxSelectedCheckBoxes = new GreeNova.JqueryLookup(this.jq, "[id$='RadTextBoxSelectedCheckBoxes']");
                this.RadComboBoxInner = new GreeNova.JqueryLookup(this.jq, "[id$='RadComboBoxInner']", true);

            })(this.get_element());
        }
        return this._controlsHolder;
    },

    initialize: function () {

        IRepairClouds.Controls.Common.RadComboCheckBoxList.callBaseMethod(this, 'initialize');

        Sys.Application.add_load(this._onLoadDelegate = this._onLoad.setCtx(this));
    },
    dispose: function () {
        Sys.Application.remove_load(this._onLoadDelegate);

        IRepairClouds.Controls.Common.RadComboCheckBoxList.callBaseMethod(this, 'dispose');
    },

    _onLoad: function () {

        var controls = this._controls();
        var h = $(controls.RadComboBoxInner.component().get_dropDownElement()).find('[id$=_HeaderItem]').empty().click(function (e) {
            e.stopPropagation();
            var chs = $(e.currentTarget).find('[name=AllItemsSelectorCheckBox]');
            var ch = chs[0];
            ch.checked = !ch.checked;
            chs.triggerHandler('click');
        } .setCtx(this));
        var chk = $('<input name="AllItemsSelectorCheckBox" type="checkbox">').click(function (e) {
            e.stopPropagation();
            this._selectAll(e.currentTarget);
        } .setCtx(this));
        var label = $('<label>All</label>');

        h.append(chk).append(label);
        var initItems = $.json.deserialize(controls.HiddenFieldClient.jq().val());
        this.bind(initItems, 'Name', 'Value');
    },

    _selectAll: function (checkBox) {

        var controls = this._controls();
        var checked = checkBox.checked;
        var items = controls.RadComboBoxInner.component().get_items();

        controls.RadTextBoxSelectedCheckBoxes.jq().val('');

        for (var i = 0; i < items.get_count(); i++) {
            var item = items.getItem(i);

            var chs = $(item.get_element()).find('[name=ItemSelectorCheckBox]')[0];
            chs.checked = checked;
            if (chs.checked) {
                if (controls.RadTextBoxSelectedCheckBoxes.jq().val() == "")
                    controls.RadTextBoxSelectedCheckBoxes.jq().val(item._text);
                else
                    controls.RadTextBoxSelectedCheckBoxes.jq().val(controls.RadTextBoxSelectedCheckBoxes.jq().val() + "\n" + item._text);
            }
        }
        this._ShowNumSelected(checked ? items.get_count() : 0);
    },

    _selectItem: function (checkBox) {
        var controls = this._controls();
        var checked = checkBox.checked;
        var items = controls.RadComboBoxInner.component().get_items();
        var itemsSelected = 0;
        var itemsTotal = items.get_count();
        controls.RadTextBoxSelectedCheckBoxes.jq().val('');
        for (var i = 0; i < items.get_count(); i++) {
            var item = items.getItem(i);
            var chs = $(item.get_element()).find('[name=ItemSelectorCheckBox]')[0];
            if (chs.checked) {
                if (controls.RadTextBoxSelectedCheckBoxes.jq().val() == "")
                    controls.RadTextBoxSelectedCheckBoxes.jq().val(item._text);
                else
                    controls.RadTextBoxSelectedCheckBoxes.jq().val(controls.RadTextBoxSelectedCheckBoxes.jq().val() + "\n" + item._text);
                itemsSelected++
            }
        }
        this._ShowNumSelected(itemsSelected, itemsTotal);
    },

    _getNumSelected: function () {

        var controls = this._controls();
        var itemsSelected = 0;
        var items = controls.RadComboBoxInner.component().get_items();
        for (var i = 0; i < items.get_count(); i++) {
            var item = items.getItem(i);
            var chs = $(item.get_element()).find('[name=ItemSelectorCheckBox]')[0];
            if (chs.checked)
                itemsSelected++
        }
        return itemsSelected;
    },

    _ShowNumSelected: function (itemsSelected, itemsTotal) {

        var controls = this._controls();
        if (GreeNova.Utils.isNoU(itemsSelected))
            itemsSelected = this._getNumSelected();
        if (GreeNova.Utils.isNoU(itemsTotal)) {
            var items = controls.RadComboBoxInner.component().get_items();
            itemsTotal = items.get_count();
        }

        var allch = $(controls.RadComboBoxInner.component().get_dropDownElement()).find('[id$=_HeaderItem] [name$=AllItemsSelectorCheckBox]')[0];
        allch.checked = (itemsSelected === itemsTotal);

        var combo = controls.RadComboBoxInner.component();
        if (itemsSelected > 0)
            combo.set_text('Selected items: ' + itemsSelected);
        else {
            combo.clearSelection();
        }
    },

    bind: function (items, itemNameField, itemValueField, nameTransform, valueTransform) {

        var controls = this._controls();
        var comboBox = controls.RadComboBoxInner.component();
        this.clear();
        var item;
        for (var i = 0; i < items.length; i++) {
            var e = items[i];
            item = new Telerik.Web.UI.RadComboBoxItem();

            var name;
            var value;
            if (nameTransform)
                name = nameTransform(e, i);
            else
                name = e[itemNameField];
            if (valueTransform)
                value = valueTransform(e, i);
            else
                value = e[itemValueField];
            item.set_value(value);
            comboBox.get_items().add(item);

            var itt = $(item.get_element()).empty().click(function (e) {
                e.stopPropagation();
                var chs = $(e.currentTarget).find('[name=ItemSelectorCheckBox]');
                var ch = chs[0];
                ch.checked = !ch.checked;
                chs.triggerHandler('click');
            } .setCtx(this));
            var chk = $('<input name="ItemSelectorCheckBox" type="checkbox">').click(function (e) {
                e.stopPropagation();
                this._selectItem(e.currentTarget);
            } .setCtx(this));
            var label = $('<label>' + name + '</label>');

            itt.append(chk).append(label);
            item._itemData = e;
        }
    },

    clear: function () {
        var controls = this._controls();
        var comboBox = controls.RadComboBoxInner.component();
        comboBox.clearItems();
        comboBox.clearSelection();
    },

    get_selectedRadItems: function () {
        var controls = this._controls();
        var res = [];
        var items = controls.RadComboBoxInner.component().get_items();
        for (var i = 0; i < items.get_count(); i++) {
            var item = items.getItem(i);
            var chs = $(item.get_element()).find('[name=ItemSelectorCheckBox]')[0];
            if (chs.checked)
                res.push(item);
        }
        return res;
    },

    get_selectedValues: function () {
        var sis = this.get_selectedRadItems();
        var res = [];
        for (var i = 0; i < sis.length; i++) {
            res.push(sis[i].get_value());
        }
        return res;
    },
    get_selectedItems: function () {
        var sis = this.get_selectedRadItems();
        var res = [];
        for (var i = 0; i < sis.length; i++) {
            res.push(sis[i].get_itemData());
        }
        return res;
    },

    select_itemsByValue: function (items) {
        var controls = this._controls();
        var combo = controls.RadComboBoxInner.component();

        controls.RadTextBoxSelectedCheckBoxes.jq().val("");

        var checks = combo.get_items().toArray();
        for (var j = 0; j < checks.length; j++) {
            var aan = $(checks[j].get_element()).find('[name=ItemSelectorCheckBox]')[0];
            aan.checked = false;
        }

        for (var i = 0; i < items.length; i++) {
            var it = combo.findItemByValue(items[i]);
            if (it) {
                var chs = $(it.get_element()).find('[name=ItemSelectorCheckBox]')[0];
                chs.checked = true;
                if (chs.checked) {
                    if (controls.RadTextBoxSelectedCheckBoxes.jq().val() == "")
                        controls.RadTextBoxSelectedCheckBoxes.jq().val(it._itemData.Name);
                    else
                        controls.RadTextBoxSelectedCheckBoxes.jq().val(controls.RadTextBoxSelectedCheckBoxes.jq().val() + "\n" + it._itemData.Name);
                }
            }
            else
                chs.checked = false;
        }
        this._ShowNumSelected();
    },

    clearSelection: function () {
        var controls = this._controls();
        var res = [];
        var items = controls.RadComboBoxInner.component().get_items();
        for (var i = 0; i < items.get_count(); i++) {
            var item = items.getItem(i);
            var chs = $(item.get_element()).find('[name=ItemSelectorCheckBox]')[0];
            chs.checked = false;
        }
        this._ShowNumSelected();
    }
}
IRepairClouds.Controls.Common.RadComboCheckBoxList.registerClass('IRepairClouds.Controls.Common.RadComboCheckBoxList', Sys.UI.Control);
